//
//  HomeViewController.m
//  ZooNgonDoiOS
//
//  Created by Trung sound on 6/4/12.
//  Copyright (c) 2012 Personal. All rights reserved.
//

#import "HomeViewController.h"
#import "ZooLoginController.h"
#import "DetailViewController.h"

@implementation HomeViewController

#define VIEW_HEADER_HEIGHT 48
#define ITEM_HEADER_WIDTH 80
#define VIEW_TABBAR_HEIGHT 42
#define ANIMATION_DURATION 0.5

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization

        _headerHomeScreenView = [[HeaderHomeScreenView alloc]initWithFrame:CGRectMake(0, 0, 320, 48)];
        
        _vTabbar = [[UIView alloc]initWithFrame:CGRectMake(0, 48, 320, 42)];
        _btnNearBy = [[UIButton alloc]init];
        _btnFeatured = [[UIButton alloc]init];
        _btnTopNew = [[UIButton alloc]init];
        _lbNearBy = [[UILabel alloc]init];
        _lbFeatured = [[UILabel alloc]init];
        _lbTopNew = [[UILabel alloc]init];   
        _vUnderTabbar = [[UIView alloc]init];
        [_vUnderTabbar setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_TABBAR]]];
        
        _vSearch = [[UIView alloc]initWithFrame:CGRectMake(10, 100, 298, 39)];
        _btnSearch = [[UIButton alloc]init ];
        _tfSearch = [[UITextField alloc]init ];
        _tfSearch.delegate =self;
        
        _vContent = [[UIView alloc]initWithFrame:CGRectMake(10, 150, 298, 275)];
        _featuredTableView = [[FeaturedTableView alloc]initWithFrame:CGRectMake(0, 0, 298, 275)];
        _featuredTableView.spotDetailDelegate = self;
        
        _footerHomeScreenView = [[FooterHomeScreenView alloc]initWithFrame:CGRectMake(0, 460 - 26, 320, 26)];

    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad {  
    
    [self.view addSubview:_headerHomeScreenView];
    
        // Draw view Tabbar
    _vTabbar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:TABBAR_BACKGROUND]];
    _btnNearBy.frame = CGRectMake(0, 0, self.view.bounds.size.width /3, VIEW_TABBAR_HEIGHT);
    [_btnNearBy setBackgroundColor:[UIColor clearColor]];
    [_btnNearBy addTarget:self action:@selector(btnNearByClicked) forControlEvents:UIControlEventTouchUpInside];

    _lbNearBy.frame = CGRectMake(20, 5, 65, 30);
    _lbNearBy.backgroundColor = [UIColor clearColor];
    [_lbNearBy setText:STRING_NEARBY];
    [_lbNearBy setFont:[UIFont fontWithName:@"Georgia" size:15]];
    [_lbNearBy setTextColor:[UIColor grayColor]];
    [_btnNearBy addSubview:_lbNearBy];

    _btnFeatured.frame = CGRectMake((self.view.bounds.size.width /3), 0, self.view.bounds.size.width /3, VIEW_TABBAR_HEIGHT);
    [_btnFeatured setBackgroundColor:[UIColor clearColor]];
    [_btnFeatured addTarget:self action:@selector(btnFeaturedClicked) forControlEvents:UIControlEventTouchUpInside];
    
    _lbFeatured.frame = CGRectMake(15, 5, 70, 30);
    _lbFeatured.backgroundColor = [UIColor clearColor];
    [_lbFeatured setText:STRING_FEATURED];
    [_lbFeatured setFont:[UIFont fontWithName:@"Georgia" size:15]];
    [_lbFeatured setTextColor:[UIColor whiteColor]];
    [_btnFeatured addSubview:_lbFeatured];

    _btnTopNew.frame = CGRectMake((self.view.bounds.size.width /3)*2, 0, self.view.bounds.size.width /3, VIEW_TABBAR_HEIGHT);
    [_btnTopNew setBackgroundColor:[UIColor clearColor]];
    [_btnTopNew addTarget:self action:@selector(_btnTopNewClicked) forControlEvents:    UIControlEventTouchUpInside];

    _lbTopNew.frame = CGRectMake(20, 5, 65, 30);
    _lbTopNew.backgroundColor = [UIColor clearColor];
    [_lbTopNew setText:STRING_TOPNEW];
    [_lbTopNew setFont:[UIFont fontWithName:@"Georgia" size:15]];
    [_lbTopNew setTextColor:[UIColor grayColor]];
    [_btnTopNew addSubview:_lbTopNew];
    
    _vUnderTabbar.frame = CGRectMake(self.view.bounds.size.width/3, VIEW_TABBAR_HEIGHT - 5, self.view.bounds.size.width/3, 5);
    
    [_vTabbar addSubview:_vUnderTabbar];
    [_vTabbar addSubview:_btnNearBy];
    [_vTabbar addSubview:_btnFeatured];
    [_vTabbar addSubview:_btnTopNew];
    
        // draw view search
    [_vSearch setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_SEARCH]]];
    [_btnSearch setBackgroundImage:[UIImage imageNamed:IMG_BTN_SEARCH] forState:UIControlStateNormal];
    _btnSearch.frame = CGRectMake(8, 8, 24, 24);
    [_vSearch addSubview:_btnSearch];
    _tfSearch.frame = CGRectMake(40, 8, 258, 30);
    [_tfSearch setPlaceholder:STRING_SEARCH_HINT];
    _tfSearch.autocorrectionType = UITextAutocorrectionTypeNo;
    [_vSearch addSubview:_btnSearch];
    [_vSearch addSubview:_tfSearch];
    
        // draw table view content
    [_vContent addSubview:_featuredTableView];
    
    [self.view addSubview:_headerHomeScreenView];
    [self.view addSubview:_vTabbar];
    [self.view addSubview:_vSearch];
    [self.view addSubview:_vContent];
    [self.view addSubview:_footerHomeScreenView];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:IMG_VIEW_BACKGROUND]];
    
    
//    [_vHeader release];
//    [_btnHome release];
//    [_btnTalk release];
//    [_btnMe release];
//    [_btnCheckin release];    
    [_headerHomeScreenView release];
    [_vTabbar release];
    [_vUnderTabbar release];
    [_btnNearBy release];
    [_btnFeatured release];
    [_btnTopNew release];
    [_lbNearBy release];
    [_lbFeatured release];
    [_lbTopNew release];
    [_footerHomeScreenView release];

}

#pragma mark - Control button clicked
-(void) btnNearByClicked {
    [UIView animateWithDuration:ANIMATION_DURATION animations:^{
        _vUnderTabbar.frame = CGRectMake(0, VIEW_TABBAR_HEIGHT - 5, self.view.bounds.size.width/3, 5);
        [_lbNearBy setTextColor:[UIColor whiteColor]];
        [_lbFeatured setTextColor:[UIColor grayColor]];
        [_lbTopNew setTextColor:[UIColor grayColor]];    
        [_vTabbar addSubview:_vUnderTabbar];
    }completion:nil];
    
    UIView *a = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 298, 275)];
    [a setBackgroundColor:[UIColor whiteColor]];
    [_vContent addSubview:a];
}

-(void) btnFeaturedClicked {
    [UIView animateWithDuration:ANIMATION_DURATION animations:^{
        _vUnderTabbar.frame = CGRectMake(self.view.bounds.size.width/3, VIEW_TABBAR_HEIGHT - 5, self.view.bounds.size.width/3, 5);
        [_lbNearBy setTextColor:[UIColor grayColor]];
        [_lbFeatured setTextColor:[UIColor whiteColor]];
        [_lbTopNew setTextColor:[UIColor grayColor]];
        [_vTabbar addSubview:_vUnderTabbar];
    }completion:nil];
    
    [_vContent addSubview:_featuredTableView];

}

-(void) _btnTopNewClicked {
    [UIView animateWithDuration:ANIMATION_DURATION animations:^{
        _vUnderTabbar.frame = CGRectMake((self.view.bounds.size.width/3)*2, VIEW_TABBAR_HEIGHT - 5, self.view.bounds.size.width/3, 5);
        [_lbNearBy setTextColor:[UIColor grayColor]];
        [_lbFeatured setTextColor:[UIColor grayColor]];
        [_lbTopNew setTextColor:[UIColor whiteColor]];
        [_vTabbar addSubview:_vUnderTabbar];
    }completion:nil];

}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    [_tfSearch resignFirstResponder];
    return 1;
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
}
*/

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) pushToLoginScreen {
    ZooLoginController *zooLoginController = [[ZooLoginController alloc]initWithNibName:@"ZooLoginController" bundle:nil ];

    [self.navigationController pushViewController:zooLoginController animated:YES];
        //    [self dismissModalViewControllerAnimated:YES];
}

/**
 * Implement delegate
 * View spot detail when select a spot
 */
- (void)pushToSpotDetailScreen:(ZooSpotBean *)spot {
    DetailViewController *_vSpotDetailViewController = [[DetailViewController alloc] initWithSpot:spot];
    _vSpotDetailViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    _vSpotDetailViewController.spotDetailDelegate = self;
    
    [self presentModalViewController:_vSpotDetailViewController animated:YES];
    
    [_vSpotDetailViewController release];
}

- (void) backToHomeView {
    [self dismissModalViewControllerAnimated:YES];
}

@end
